package com.woniuxy.yoga.progress.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.CoachState;
import com.woniuxy.yoga.commons.core.statedata.CourseState;
import com.woniuxy.yoga.commons.core.statedata.ProgressState;
import com.woniuxy.yoga.progress.client.CoachClient;
import com.woniuxy.yoga.progress.client.CourseClient;
import com.woniuxy.yoga.progress.client.UserClient;
import com.woniuxy.yoga.progress.dao.model.TeamRegist;
import com.woniuxy.yoga.progress.dao.mapper.TeamRegistMapper;
import com.woniuxy.yoga.progress.model.dto.Coach;
import com.woniuxy.yoga.progress.model.dto.TeamCourseDTO;
import com.woniuxy.yoga.progress.model.dto.UserInfoDto;
import com.woniuxy.yoga.progress.model.exception.ProgressException;
import com.woniuxy.yoga.progress.model.exception.ProgressExceptionCode;
import com.woniuxy.yoga.progress.model.param.TeamRegisterParam;
import com.woniuxy.yoga.progress.service.TeamRegistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 团课上课记录表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class TeamRegistServiceImpl extends ServiceImpl<TeamRegistMapper, TeamRegist> implements TeamRegistService {

    @Autowired
    private TeamRegistMapper teamRegistMapper;

    @Autowired
    private CoachClient coachClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private CourseClient courseClient;

    /**
     * 上课记录
     * @param param 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void teamRegister(TeamRegisterParam param) {
        //查询教练状态是否正常
        Result<Coach> byCoachId = coachClient.getByCoachId(param.getCoachId());
        if (byCoachId.getCode()!=200) throw new ProgressException(byCoachId.getCode(),byCoachId.getMessage());
        if (!Objects.equals(byCoachId.getData().getCoachState(), CoachState.COACH_STATE_NORMAL))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_COACH_STATE_WRONG);
        //查询团课状态
        Result<TeamCourseDTO> byTeamCourseId = courseClient.getByTeamCourseId(param.getTeamCourseId());
        if (byTeamCourseId.getCode()!=200) throw new ProgressException(byTeamCourseId.getCode(),byTeamCourseId.getMessage());
        if (!Objects.equals(byTeamCourseId.getData().getTeamCourseState(), CourseState.TEAM_COURSE_STATE_HAVE))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_TEA_CARD_USED);
        //查询用户状态
        for (Integer userId : param.getAbsentUser()) {
            Result<UserInfoDto> result = userClient.getByUserId(userId);
            if (result.getCode()!=200) throw new ProgressException(result.getCode(),result.getMessage());
        }
        //创建json字符串保存未到用户集合
        String absentUser = JSONUtil.toJsonStr(param.getAbsentUser());
        //创建对象存储
        TeamRegist teamRegist = new TeamRegist();
        teamRegist.setRegistState(ProgressState.TEAM_REGIST_STATE_NO);
        teamRegist.setRegistTime(DateUtil.format(new Date(System.currentTimeMillis()),"yyyy-MM-dd HH:mm:ss"));
        teamRegist.setTeamCourseId(param.getTeamCourseId());
        teamRegist.setAdsentUser(absentUser);
        teamRegist.setClassCount(param.getClassCount());
        teamRegist.setCoachId(param.getCoachId());
        teamRegist.setTeamProductId(byTeamCourseId.getData().getTeamProductId());
        teamRegistMapper.insert(teamRegist);
    }
}
